package jmine.tec.web.wicket.component.table;

import java.io.Serializable;
import java.util.Set;

import org.apache.wicket.model.IModel;

/**
 * Model que adiciona elementos ao um set.
 * 
 * @author Rafael Volpato
 * @date Sep 28, 2011
 */
public class SelectEntityModel implements IModel<Boolean> {

    private final Set<Serializable> chosen;

    private final Serializable primaryKey;

    /**
     * Construtor.
     * 
     * @param chosen {@link Set} que armazenas elementos serializados.
     * @param primaryKey chave primaria do elemento
     */
    public SelectEntityModel(Set<Serializable> chosen, Serializable primaryKey) {
        this.chosen = chosen;
        this.primaryKey = primaryKey;
    }

    /**
     * {@inheritDoc}
     */
    public Boolean getObject() {
        return this.chosen.contains(this.primaryKey);
    }

    /**
     * {@inheritDoc}
     */
    public void setObject(Boolean object) {
        if (object.booleanValue()) {
            this.chosen.add(this.primaryKey);
        } else {
            this.chosen.remove(this.primaryKey);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void detach() {
    }

    /**
     * @return the primaryKey
     */
    public Serializable getPrimaryKey() {
        return this.primaryKey;
    }
}
